package cn.imvc.app.models;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Express;
import cn.imvc.app.entities.ExpressFee;
import cn.imvc.app.repositories.ExpressFeeRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;
@Service
public class ExpressFeeModelImpl implements ExpressFeeModel{

	@Autowired
	ExpressFeeRepository expressFeeRepository;
	
	public Page<ExpressFee> find(ViewRequest viewRequest) {
		
		return expressFeeRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public ExpressFee save(int expressId, int areaId,ExpressFee expressFee) {
		
		return expressFeeRepository.saveAndFlush(expressFee);
	}

	public ExpressFee get(int id) {
		
		return expressFeeRepository.getOne(id);
	}

	public ExpressFee update(int id, int expressId, int areaId,ExpressFee expressFee) {
		ExpressFee loadExpressFee = expressFeeRepository.getOne(id);
		BeanUtils.copyProperties(expressFee,loadExpressFee, new String[]{"id"});
		return expressFeeRepository.saveAndFlush(loadExpressFee);
	}

	public void delete(int id) {
		expressFeeRepository.delete(id);
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			expressFeeRepository.delete(id);
		}
		
	}

	@Override
	public ExpressFee findByExpressAndAreaId(Express express, int areaId) {
		return expressFeeRepository.findByExpressAndAreaId(express, areaId);
	}



}
